﻿Ext.namespace('EHSubmit.BBS.AddAnn');



//------------发表新帖子-----------
EHSubmit.BBS.AddAnn.SendAnn = function SendAnn() {
    var frm = Ext.getCmp('AddAnnForm');
    if (frm.getForm().isValid()) { // 验证表单
        var ann = EHSubmit.BBS.AnnData;
        var titleTextfield = Ext.getCmp('title');
        var body = Ext.getCmp('innerForm_down').getValue();
        var departmentname = Ext.getCmp('departmentName').getValue();
        var departmentid = Ext.getCmp('AddAnn_deptCombo').getValue();
        //aid自增,其他服务器端处理
        var Announce = {
            Topic: titleTextfield.getValue(),
            Body: body,
            UserID: CurrentUser.ID,      
            UserName: CurrentUser.Name,
            DepartmentID: departmentid,
            DepartmentName: departmentname
        }
        var AnnJson = Ext.util.JSON.encode(Announce);

        frm.getForm().submit({     // 提交
                url: EHSubmit.ajaxRoot + 'BBSHandler.ashx?op=add',
                waitTitle: '等待',
                waitMsg: '正在发送，请稍后...',
                method: 'POST',
                params: {
                    Ann: AnnJson
                },
                success: function(form, action) {
                    var successed = action.result.success;
                    if (successed) {
                        ///////////////  提交成功处理  ///////////////////
                        Ext.MessageBox.alert('提示', action.result.msg);
                        Ext.getCmp('AnnGrid').store.reload();
                    }
                    else {
                        Ext.MessageBox.alert('失败', '信息:' + action.result.msg);
                    }
                },
                failure: function(form, action) {
                    var errmsg = action.result.msg;
                    Ext.MessageBox.alert('错误', errmsg);
                }   // failure  
            });
            //-----------关闭发件窗口------------
            var thisWin = Ext.getCmp('addAnnWin');
            thisWin.close();

        }  
    };


//---------------留言主窗口的上部----------
EHSubmit.BBS.AddAnn.innerForm_up = {
    id: 'innerForm_up',
    xtype: 'fieldset',
    flex: 0.25,
    //height:80,
    labelAlign: 'center',
    //anchor:'100%',
    defaults: { width: 400 },
    defaultType: 'textfield',
    items: [{
        id: 'title',
        fieldLabel: '主题',
        //name: 'title',
        allowBlank: false,
        blankText: '此处不能为空'
    }, {
        id:'form_combo', 
        xtype: 'compositefield',
        fieldLabel: '请选择',
        combineErrors: false,
        items:[{
            xtype:'displayfield',
            value:'学院'
//        },{
//            id: 'instituteselect',
//            xtype: 'combo',
//            emptyText: '请选择...',
//            mode: 'local',
//            triggerAction: 'all',
//            selectOnFocus: true,
//            valueField: 'InstituteID',
//            displayField: 'InstituteName',
//            listClass: 'x-menu',
//            width: 165,
//            editable: false,
//            store: new Ext.data.JsonStore({
//                storeId: 'InstituteStore',
//                //autoLoad: true,
//                baseParams: { Op: 'GetInstitute' },     //根据教师id获取其教的课程
//                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'BBSHandler.ashx' }),  //后台具体处理

//                root: 'root',
//                fields: [
//                    { name: 'InstituteID', mapping: 'InstituteID' },
//                    { name: 'InstituteName', mapping: 'InstituteName' }
//                ]
//            }),
//            listeners: {
//                select: function(combo,record) {
//                    var derpartmentcombo = Ext.getCmp('departmentselect');
//                    var store = derpartmentcombo.getStore();
//                    store.setBaseParam('InstituteID', record.data.InstituteID);
//                    store.load();   
//                    } 
//             }
        },{
            xtype: 'combo',
            id:'AddAnn_instituteCombo',
            name: 'institute',
            fieldLabel: '学院',
            editable: false,
            emptyText: '请选择...',
            blankText: '请选择...',
            width: 165,
            allowBlank:false,
            mode: 'remote',
            triggerAction: 'all',
            selectOnFocus: true,
            valueField: 'instituteID',
            displayField: 'instituteName',

            store: new Ext.data.JsonStore({
                storeId: 'instituteStore',
                //autoLoad: true,
                baseParams: { Op: 'List_Institute' },     //根据教师id获取其教的课程
                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'BBSHandler.ashx' }),  //后台具体处理

                root: 'institutes',
                fields: [
                    { name: 'instituteID', mapping: 'InstituteID' },
                    { name: 'instituteName', mapping: 'InstituteName' }
                ]
            }),

            listeners: {
                select: function(combo, record, index) {
                    //根据学院Id获取专业信息
                        Ext.getCmp('AddAnn_deptCombo').clearValue();
                        Ext.getCmp('AddAnn_deptCombo').enable();
                }
            }
        
        },{
            xtype:'displayfield',
            value:'专业'
//        },{
//            id: 'departmentselect',
//            xtype: 'combo',
//            emptyText: '请选择...',
//            mode: 'remote',
//            triggerAction: 'all',
//            selectOnFocus: true,
//            valueField: 'InstituteID',
//            displayField: 'InstituteName',
//            listClass: 'x-menu',
//            width: 165,
//            editable: false,
//            store: new Ext.data.JsonStore({
//                storeId: 'DerpartmentStore',
//                autoLoad: false,
//                baseParams: { Op: 'GetDerpartment', InstituteID: '' },
//                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'BBSHandler.ashx' }),  //后台具体处理

//                root: 'root',
//                listeners: {
//                    beforeload: function(thisStore) {
//                        thisStore.removeAll();
//                    }
//                },
//                fields: [
//                    { name: 'DerpartmentID', mapping: 'DerpartmentID' },
//                    { name: 'DerpartmentName', mapping: 'DerpartmentName' }
//                ]     
//            }),
//            listeners: {
//                select:function(combo,record) {
//                    var departmentid = Ext.getCmp('departmentid');
//                    departmentid.setValue(record.data.DerpartmentID);
//                }
//            }
        }, {
            xtype: 'combo',
            id: 'AddAnn_deptCombo',
            name: 'AddAnn_department',
            fieldLabel: '专业',
            disabled: true,     //初始化时不可用，选择课程后可用
            editable: false,
            emptyText: '请选择...',
            blankText: '请选择...',
            allowBlank: false,
            mode: 'local',
            width: 165,
            pruneModifiedRecords: true,
            triggerAction: 'all',
            autoSelect: false,
            selectOnFocus: true,
            valueField: 'deptID',
            displayField: 'deptName',

            store: new Ext.data.JsonStore({
                storeId: 'deptStore',
                autoLoad: false,
                baseParams: { Op: 'List_Depts', insID: '' },
                proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'BBSHandler.ashx' }),  //后台具体处理
                pruneModifiedRecords: true,
                totalProperty: 'total',    //总记录个数
                root: 'depts',
                fields: [
                                    { name: 'deptID', mapping: 'DepartmentID' },
                                    { name: 'deptName', mapping: 'DepartmentName' }
                                ],
                listeners: {
                    beforeload: function(thisStore) {
                        thisStore.removeAll();
                    },
                    load: function(thisStore, records) {
                        //----判断有无数据---------
                    var deptCombo = Ext.getCmp('AddAnn_deptCombo');
                        if (thisStore.getTotalCount() == 0) {
                            //deptCombo.disable();
                            deptCombo.markInvalid('无相关数据!');
                        }
                        else {
                            deptCombo.setValue('请选择...');
                        }
                    }
                }
            }),

            listeners: {
                enable: function(combo) {
                    //将学院id传给服务器端
                    var store = combo.getStore();
                    var insID = Ext.getCmp('AddAnn_instituteCombo').getValue();
                    store.setBaseParam('insID', insID);
                    store.load();
                },
                select: function(combo, record, index) {

                    Ext.getCmp('departmentName').setValue(record.data.deptName);
                }
            }
        
        },{
            xtype:'hidden',
            id:'departmentName',
            value:''
        }]
    }]
    };

    //----------------留言主窗口的下部-------------
    EHSubmit.BBS.AddAnn.innerForm_down = {
        xtype:'panel',
        flex:0.9,
        layout:'fit',
        items:[{
            id:'innerForm_down',
            xtype: 'htmleditor',
            //name:'content',
            emptyText: '问题内容',
            flex:1
        }]
  

    };

    //-------------留言主窗口----------
    EHSubmit.BBS.AddAnn.innerForm = {
        xtype: 'form',
        id: 'AddAnnForm',
        layout: {
            type:'vbox',
            padding:'2',
            align:'stretch'
        },
        border:false,
        frame: true,
        monitorValid: true,
        //-------中间部分----------
        items: [EHSubmit.BBS.AddAnn.innerForm_up, EHSubmit.BBS.AddAnn.innerForm_down]

    };

EHSubmit.BBS.AddAnn.window = {
    id: 'addAnnWin',
    title: '提问',
    //xtype:'window',
    width: 700,
    height: 450,
    plain: true,
    border: false,
    layout: 'fit',
    modal: true,

    //-------topbar-------
    tbar: [{
        text: '发表',
        tooltip: '发表',
        iconCls: 'icon-send',
        handler: EHSubmit.BBS.AddAnn.SendAnn
    }, '-', {
        text: '关闭',
        tooltip: '关闭',
        iconCls: 'close',
        handler: function() {
            var addAnnWin = Ext.getCmp('addAnnWin');
            addAnnWin.close();
        }
    }],

        items: [EHSubmit.BBS.AddAnn.innerForm],

        //-------footbar--------------
        fbar: [{
            text: '发表',
            tooltip: '发表',
            iconCls: 'icon-send',     //图标引用需写在css中
            handler: EHSubmit.BBS.AddAnn.SendAnn
        }, '-', {
            text: '关闭',
            tooltip: '关闭',
            iconCls: 'close',
            handler: function() {
                //----关闭当前窗口---------
                var addAnnWin = Ext.getCmp('addAnnWin');
                addAnnWin.close();
            }
    }]
 };